package cn.com.ddapp.ddm.widget.jazzyLoopViewPager;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.Banner;
import cn.com.ddapp.ddm.ui.AboutActivity;
import cn.com.ddapp.ddm.ui.CommidContentActivity;
import cn.com.ddapp.ddm.ui.ShareImgActivity;
import cn.com.ddapp.ddm.util.StringUtils;


/**
 * @author gw 2015-9-9
 */
public class BannerViewPagerAdapter extends PagerAdapter {
	private static final String TAG = BannerViewPagerAdapter.class
			.getSimpleName();
	/**
	 * 图片资源列表
	 */
	private ArrayList<Banner> mAdList = new ArrayList<Banner>();
	private Context context;
	private JazzyViewPager mJazzy;
	private String type;
	public BannerViewPagerAdapter(JazzyViewPager mJazzy, Context context,
								  ArrayList<Banner> adList,String type) {
		this.context = context;
		this.mAdList = adList;
		this.mJazzy = mJazzy;
		this.type = type;
	}

	@Override
	public int getCount() {
		return mAdList.size();
		// return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		// if (view instanceof OutlineContainer) {
		// return ((OutlineContainer) view).getChildAt(0) == obj;
		// } else {
		// return view == obj;
		// }
		return view == obj;
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		ViewHolder holder = null;
		// Log.e(TAG,"mAdList.size()==0");
		if (mAdList.size() == 0) {
			return container;
		}
		// final String imageUrl = mAdList.get(position %
		// mAdList.size()).getImgs();

		View view = LayoutInflater.from(context).inflate(
				R.layout.loop_viewpager_item, null);
		ImageView imageView = (ImageView) view
				.findViewById(R.id.header_imageview);
		imageView.setScaleType(ScaleType.CENTER_CROP);

		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// land do nothing is ok
			Glide.with(context).load(mAdList.get(position).getImgPath()).into(imageView);
		} else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			Glide.with(context).load(mAdList.get(position).getImgPath()).into(imageView);
		}

        final ArrayList<String> arrayList = new ArrayList<String>();
        for(int a=0;a<mAdList.size();a++){
            arrayList.add(mAdList.get(a).getImgUrl());

        }
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out.println("mAdList.get(position).getLink().substring(0,2))" +mAdList.get(position).getLink());
				if(type.equals("home")){
					if(StringUtils.isUrl(mAdList.get(position).getLink())){//外部链接
						Intent it = new Intent();
						it.setAction("android.intent.action.VIEW");
						Uri content_url = Uri.parse(mAdList.get(position).getLink());
						it.setData(content_url);
						context.startActivity(it);
					}else if("a/".equals(mAdList.get(position).getLink().substring(0,2))){//文章
						Intent intent = new Intent(context,AboutActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString("category", "4");
						bundle.putString("data",mAdList.get(position).getLink().substring(2,mAdList.get(position).getLink().length()));
						intent.putExtra("ABOUT", bundle);
						context.startActivity(intent);
					}else if("g/".equals(mAdList.get(position).getLink().substring(0,2))){//商品详情
						Intent intent = new Intent(context, CommidContentActivity.class);
						Log.e("IDS", "ID===========" + mAdList.get(position).getLink().substring(2,mAdList.get(position).getLink().length()));
						intent.putExtra("type","id");
						intent.putExtra("id", mAdList.get(position).getLink().substring(2,mAdList.get(position).getLink().length()));

						context.startActivity(intent);
					}else if(mAdList.get(position).getLink().contains("javascript:")){//不跳转

					}
				}else{
					ShareImgActivity.startIntent(context,arrayList,position);
				}

//                ShareImgActivity.startIntent(context,arrayList,position);
//				TipsToast.makeText(context, "" + position, TipsToast.LENGTH_SHORT).show();
//                Log.i("aaa","---aa--"+mAdList.get(position).getImgUrl());
            }
		});
		// imageView.setBackgroundResource(R.drawable.image);
		container.addView(view);
		// mJazzy.setObjectForPosition(imageView, position);
		return view;
	}

	public static class ViewHolder {
		public View view;
		public ImageView imageView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// 这里不需要做任何事情
		// container.removeView((View)object);
	}

}
